#pragma once

template<class T>
class EnginePointer
{
public:
	EnginePointer(void)
	{
		obj = 0;
	}

	EnginePointer(T* o)
	{
		obj = 0;
		*this=o;
	}
	
	EnginePointer(const EnginePointer<T>& p)
	{
		obj = 0;
		*this=p;
	}
	
	~EnginePointer(void)
	{
		if (obj) obj->Release();
	}

	void operator=(T* o)
	{
		if (obj) obj->Release();
			obj=o;
		if (obj) obj->AddRef();
	}

	void operator=(const EnginePointer<T>& p)
	{	
		if (obj) obj->Release();
			obj=p.obj;
		if (obj) obj->AddRef();
	}
	
	inline T& operator*() const
	{
		return *obj;
	}
	
	inline T* operator->() const
	{
		return obj;
	}
	
	inline operator T*() const
	{
		return obj;
	}

	inline operator EnginePointer<T>&()
	{
		return *this;
	}

	template<class K>
	inline operator EnginePointer<K>() const
	{
		return (K*)obj;
	}

	inline T** innerAddress()
	{
		return &obj;
	}
	
	inline bool isValid() const
	{
		return obj!=0;
	}
	
	inline bool operator!()
	{
		return !obj;
	}
	
	inline bool operator==(const T* o) const
	{
		return obj==o;
	}
	
	inline bool operator==(const EnginePointer<T>& p) const
	{
		return obj == p.obj;
	}
protected:
	T* obj;
};